package com.fzu.express.controller;


import com.fzu.express.annotation.SystemLog;
import com.fzu.express.entity.AddressBook;
import com.fzu.express.entity.WebUser;
import com.fzu.express.service.AddressBookService;
import com.fzu.express.utils.JwtUtil;
import com.fzu.express.utils.PageVO;
import com.fzu.express.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2022-10-27
 */
@RestController
@RequestMapping("/address-book")
public class AddressBookController {
    @Resource
    private AddressBookService addressBookService;

    /**
     * 分页条件查询
     * @param addressBook 参数
     * @param pageNum 页码
     * @param pageSize 记录数
     * @return
     */
    @GetMapping
    public R list(AddressBook addressBook, @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                  @RequestHeader("token") String token) {
        WebUser user = JwtUtil.getUser(token);
        addressBook.setUserId(user.getId());
        PageVO pageVO = addressBookService.listAddressBook(addressBook, pageNum, pageSize);
        return R.ok().data(pageVO);
    }

    /**
     * 新增地址簿
     * @param addressBook 参数
     * @return R
     */
    @PostMapping
    @SystemLog(businessName = "新增地址簿")
    public R add(@RequestBody AddressBook addressBook, @RequestHeader("token") String token) {
        WebUser user = JwtUtil.getUser(token);
        addressBook.setUserId(user.getId());
        addressBookService.addAddressBook(addressBook);
        return R.ok();
    }

    /**
     * 修改
     * @param addressBook 参数
     * @return
     */
    @PutMapping
    @SystemLog(businessName = "修改地址簿")
    public R update(@RequestBody AddressBook addressBook) {
        addressBookService.updateAddressBook(addressBook);
        return R.ok();
    }

    /**
     * 删除
     * @param id 地址id
     * @return
     */
    @DeleteMapping("/{id}")
    @SystemLog(businessName = "删除地址簿")
    public R delete(@PathVariable("id") Long id) {
        addressBookService.deleteAddressBook(id);
        return R.ok();
    }


}

